package com.lonty.vote.service;

import com.lonty.vote.mapper.master.VoteContentMapper;
import com.lonty.vote.mapper.master.VoteRecordsMapper;
import com.lonty.vote.model.VoteContent;
import com.lonty.vote.request.VoteContentList;
import com.lonty.vote.vo.VoteContentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/5/25 10:14
 * @Version 1.0
 */
@Service
public class VoteContentService {

    @Resource
    private VoteContentMapper voteContentMapper;

    @Resource
    private VoteRecordsMapper voteRecordsMapper;

    /**
     * 保存投票内容
     * @param voteContentVo
     * @return
     */
    public boolean saveVoteContent(VoteContentVo voteContentVo){
        VoteContent voteContent = new VoteContent();
        BeanUtils.copyProperties(voteContentVo, voteContent);
        return voteContentMapper.saveVoteContent(voteContent) > 0;
    }

    /**
     * 获取投票内容
     * @return
     */
    public VoteContentList getVoteContent(){
        List<VoteContent> voteContents01 = voteContentMapper.getVoteContent(1);
        for(int i = 0;i < voteContents01.size();i++){
            int num = voteRecordsMapper.countVoteContent(voteContents01.get(i).getId());
            voteContents01.get(i).setVoteNum(num);
        }
        List<VoteContent> voteContents02 = voteContentMapper.getVoteContent(2);
        for(int i = 0;i < voteContents02.size();i++){
            int num = voteRecordsMapper.countVoteContent(voteContents02.get(i).getId());
            voteContents02.get(i).setVoteNum(num);
        }
        List<VoteContent> voteContents03 = voteContentMapper.getVoteContent(3);
        for(int i = 0;i < voteContents03.size();i++){
            int num = voteRecordsMapper.countVoteContent(voteContents03.get(i).getId());
            voteContents03.get(i).setVoteNum(num);
        }
        VoteContentList voteContentList = VoteContentList.builder()
                .voteContents01(voteContents01)
                .voteContents02(voteContents02)
                .voteContents03(voteContents03)
                .build();
        return voteContentList;
    }

}
